<?php
/**
 * @file obtain.php 从数据库中随机取出4条推广代码（2条table，2条文字链接）
 * @package taoke_html
 * @subpackage 
 * @version $id: 0.1 UTF-8 2009-3-14 上午12:56:39 $
 * @author LD King (mifunny.info)
 * @copyright Copyright (c) 2007 - 2009 MiFunny China Inc. (http://mifunny.info)
 */
define('DB_FILE', '7tao.db');

// 说明：计算 UTF-8 字符串长度（忽略字节的方案）
// 整理：http://www.CodeBit.cn 
function utf8_strlen($str) {
	$i = 0;
	$count = 0;
	$len = strlen ($str);
	while ($i < $len){
		$chr = ord ($str[$i]);
		$count++;
		$i++;
		if($i >= $len) break;
		if($chr & 0x80) {
			$chr <<= 1;
			while ($chr & 0x80) {
				$i++;
				$chr <<= 1;
			}
		}
	}
	return $count;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="zh-CN">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>随机淘客代码抽取 | 水草树</title> 
<meta name="robots" content="nofollow" />
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
</head>
<body>
<div id="wrapper">
	<div id="header">
		<p><a href="cut.php">数据切割</a> | <a href="submit.php">提交淘客数据库</a> | <a href="obtain.php">随机取出淘客代码</a></p>
	</div>
<?php 
// 连接 SQLite 
try {
	$dbh = new PDO("sqlite:".DB_FILE, null, null, array(PDO::ATTR_PERSISTENT => true));
}catch (PDOException $ex){
	die("[SQLite Connect Error]: ".$ex->getMessage().'<br />\n');
}
$dbh->query('PRAGMA encoding = "UTF-8"');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sqlSelect = "SELECT i_url, i_title, i_table, title, url FROM taoke ORDER BY random() LIMIT 5";
$result = $dbh->query($sqlSelect);
$rows = $result->fetchAll(PDO::FETCH_BOTH);
if (empty($rows[0])){ //已含有URL
	exit('<div class="box error">数据取得为空！</div>');
}
$show = array();
for ($i=0; $i<count($rows); $i++) {
	$show[$i]['HTML'] = $rows[$i]['i_table'];
	$show[$i]['URL'] = '<a style="font-size:16px;" ';
	if (utf8_strlen($rows[$i]['i_url']) < 5 ){
		$show[$i]['URL'] .= 'href="'.$rows[$i]['url'].'" ';
	}else {
		$show[$i]['URL'] .= 'href="'.$rows[$i]['i_url'].'" ';
	}
	if (utf8_strlen($rows[$i]['i_title']) < 5 ){
		$show[$i]['URL'] .= 'title="'.$rows[$i]['title'].'" >'.$rows[$i]['title'].'</a>';
	}else {
		$show[$i]['URL'] .= 'title="'.$rows[$i]['i_title'].'" >'.$rows[$i]['i_title'].'</a>';
	}
}

$showbox = '<table style="width:580px;text-align:center;">
				<tr><td>'.$show[0]['HTML'].'</td><td>'.$show[1]['HTML'].'</td></tr>
				<tr><td>'.$show[2]['URL'].'</td><td>'.$show[3]['URL'].'</td></tr>
			</table>'; 
?>
<div class="showbox"><!-- Begin: --><?php echo "\t\n\t\n\t\n\t\n\t\n\t\n\t\n".$showbox."\t\n\t\n\t\n\t\n\t\n\t\n\t\n"; ?><!-- END --></div>
<div class="showbox"><?php echo htmlspecialchars($showbox); ?></div>

<div class="showbox"><?php echo $show[4]['HTML'].$show[4]['URL']; ?></div>

</div><!-- #wrapper -->
</body>
</html>